/******************************************************************************
 * Copyright (C) 2016  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef NODE_SYSTEM_EVENT_POSIX_H_
#define NODE_SYSTEM_EVENT_POSIX_H_

#include <pthread.h>
#include <node/noncopyable.h>

namespace node {
	namespace system {

	class event_impl : private noncopyable
	{
	public:
		struct sys_event {
			bool state;
			bool manual_reset;
			pthread_mutex_t mutex;
			pthread_cond_t cond;
		};

	protected:
		event_impl(bool init_state, bool auto_reset);
		~event_impl(void);

		bool set_impl(void);
		bool reset_impl(void);
		bool wait_impl(void);
		bool wait_impl(long milliseconds);

	private:
		static struct sys_event* create_event(bool init_state, bool auto_reset);
		static void destroy_event(struct sys_event* event);
		static bool set_event(struct sys_event* event);
		static bool reset_event(struct sys_event* event);
		static bool wait_event(struct sys_event* event);
		static bool wait_event(struct sys_event* event, long milliseconds);

		struct sys_event* _event;
	};

	inline bool event_impl::set_impl(void)
	{
		return (event_impl::set_event(_event) == 0);
	}
	inline bool event_impl::reset_impl(void)
	{
		return (event_impl::reset_event(_event) == 0);
	}
	inline bool event_impl::wait_impl(void)
	{
		return (event_impl::wait_event(_event) == 0);
	}
	inline bool event_impl::wait_impl(long milliseconds)
	{
		return (event_impl::wait_event(_event, milliseconds) == 0);
	}

}}

#endif // NODE_SYSTEM_EVENT_POSIX_H_